package com.example.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.shop.R;
import com.example.shop.base.SCBaseActivity;
import com.example.shop.fragment.FenLeiFragment;
import com.example.shop.fragment.GDFragment;
import com.example.shop.fragment.GWCFragment;
import com.example.shop.fragment.HomeFragment;
import com.example.shop.fragment.SearchFragment;
import com.example.shop.fragment.WelcomeFragment;
import com.example.sjlibrary.base.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HomeActivity extends SCBaseActivity implements RadioGroup.OnCheckedChangeListener {


    @Bind(R.id.layout_home_frag_content)
    FrameLayout mLayoutHomeFragContent;
    RadioGroup mRgNavi;
    HomeFragment mHomeFragment=new HomeFragment();
    FenLeiFragment mFenLeiFragment=new FenLeiFragment();
    SearchFragment mSearchFragment=new SearchFragment();
    GWCFragment mGWCFragment=new GWCFragment();
    GDFragment mGDFragment=new GDFragment();
    BaseFragment[] mAllFragments={mHomeFragment,mFenLeiFragment,mSearchFragment,mGWCFragment,mGDFragment};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        this.addFragment(R.id.layout_home_frag_content,mHomeFragment);
        this.addFragment(R.id.layout_root_home,new WelcomeFragment());
        mRgNavi= (RadioGroup) this.findViewById(R.id.navi);
        mRgNavi.setOnCheckedChangeListener(this);
        mRgNavi.requestFocus();

    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        BaseFragment showFragment=null;
        switch (checkedId) {
            case R.id.rb_navi_shouye:
                showFragment=mHomeFragment;
               break;
            case R.id.rb_navi_fenlei:
                showFragment=mFenLeiFragment;
               break;
            case R.id.rb_navi_sousuo:
                showFragment=mSearchFragment;
               break;
            case R.id.rb_navi_gwc:
                showFragment=mGWCFragment;
               break;
            case R.id.rb_navi_gengduo:
                showFragment=mGDFragment;
               break;
            default:
                break;
        }
        changeFragShowAndHide(showFragment);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ((RadioButton) mRgNavi.getChildAt(3)).setChecked(true);
    }
    public void changeFragShowAndHide(BaseFragment showFragment){
        for (BaseFragment baseFragment:mAllFragments){
            if (baseFragment==showFragment){
                if (!baseFragment.isAdded()){
                    addFragment(R.id.layout_home_frag_content,baseFragment);
                }else {
                    showFragment(baseFragment);
                }
            }else {
                if (baseFragment.isAdded()){
                    hideFragment(baseFragment);
                }
            }
        }

    }


}
